/**
 * Created by liuqingxia on 2016/4/11.
 */


/**
 * 点击数据源选择的插入按钮
 */
function clickSourceInsertBtn(selectDiv,type) {

    $(document).on("click", "." + type, function () {

        var dataSource = $.trim($(this).parent().find("span.dataSource").html());
        var selectDivId = $.trim($(this).parent().parent().attr("id"));

        var dataSourceHtml = "" +
            "<span class='dataSource-row' style='overflow: hidden'>" +
                "<button class='removeBtn datasource-use-btn'>删除</button>" +
                "<span class='dataSource'>" + "&nbsp;" +
                    dataSource +
                "</span>" +
            "</span>";


        $("#" + selectDiv).append(dataSourceHtml);


    });
}


/**
 * 点击数据源选择的删除按钮
 */
function clickSourceRemoveBtn() {
    $(document).on("click", ".removeBtn", function () {
        $(this).parent().remove();
    });
}

/**
 * 闭包，allDataSourceDiv
 * @param divId
 * @param findAllUrl
 * @returns {{init: init, getAllDataSources: getAllDataSources}}
 */
function getAllDataSourceDiv(divId, findAllUrl,type) {

    var divId = divId;
    var findAllUrl = findAllUrl;

    /**
     * 获取所有的数据源列表
     * @returns {Array}
     */
    function getAllDataSources() {
        var dataSources = [];
        var url = getUrlBySuffix(findAllUrl);
        try {
            $.postHttpSync({}, url, function (json) {
                if (json && json.success) {
                    dataSources = json['list'];
                } else {
                    console.error("获取数据源数组失败");
                }
            });
        } catch (e) {
            console.error("获取数据源数组失败，" + e.message);
        }
        return dataSources;
    }

    /**
     * 将dataSource数组与html渲染
     * @param dataSources
     * @returns {Array}
     */
    function getDataSourceHtml(dataSources) {
        var dataSourceHtml = [];
        for (var i = 0; i < dataSources.length; i++) {
            var dataSource = dataSources[i];
            var dataSourceRow = "" +
                "<span class='dataSource-row'>" +
                    "<button class='insertBtn " + type +" datasource-use-btn'>添加</button>" +
                    "<span class='dataSource'>" +
                    "<span class='dataSourceId'>" +
                        dataSource['id'] +
                    "</span>" + "&nbsp;" +
                        dataSourceName(dataSource['userName'], dataSource['jdbcUrl']) +
                    "</span>" +
                "</span>";

            dataSourceHtml.push(dataSourceRow);
        }
        return dataSourceHtml;
    }

    /**
     * 初始化allDataSourceDiv
     */
    function init() {
        var dataSources = getAllDataSources();
        var dataSourceHtml = getDataSourceHtml(dataSources);
        var allDataSourceDiv = $("#" + divId);
        allDataSourceDiv.empty();
        for (var i in dataSourceHtml) {
            allDataSourceDiv.append(dataSourceHtml[i]);
        }
    }

    return {
        init: init,
        getAllDataSources: getAllDataSources
    };

}

